package com.mobile.check.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.List;

import cn.droidlover.xdroidmvp.kit.NLog;
import com.mobile.check.app.R;


/**
 * 作者：李学伟
 * 日期：2018/8/10
 * 用途：
 */

public abstract class BaseView extends LinearLayout {
    public TextView labelTv;
    public View mView;
    public LinearLayout mainLayout;
    public Context mContext;
    TextView valueTv;
    String label;
    String paramer;
    String parentParamer;
    String relevanceParamer;
    String mustParamer;
    String type;
    private boolean isMust;//是否必填
    int labelColor;
    int contentColor;
    int minlines;
    int inputType;
    int maxLength;
    int orientation;
    String defaultValue;


    public BaseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.baseView);//获取配置属性
        label = tArray.getString(R.styleable.baseView_label);
        labelColor = tArray.getColor(R.styleable.baseView_labelColor, getResources().getColor(R.color.blackAlpha30));
        contentColor = tArray.getColor(R.styleable.baseView_contentColor, getResources().getColor(R.color.text_color));
        minlines = tArray.getInt(R.styleable.baseView_minLine, 1);
        inputType = tArray.getInt(R.styleable.baseView_inputType, 0);
        maxLength = tArray.getInt(R.styleable.baseView_maxLength, 0);
        orientation = tArray.getInt(R.styleable.baseView_orientation, 0);
        tArray.recycle();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(getViewResourceId(), this);
        mContext = context;
        labelTv = mView.findViewById(R.id.labelTv);
        valueTv = mView.findViewById(R.id.valueTv);
        labelTv.setTextColor(labelColor);
        labelTv.setText(label);
        NLog.d("minlines","label="+label+" minlines="+minlines);
        setValueTvStatus();
    }

    protected void setValueTvStatus(){
        if (null != valueTv) {
            valueTv.setTextColor(contentColor);
            valueTv.setMinLines(minlines);
            if (minlines == 1) {
                valueTv.setSingleLine(true);
            } else {
                valueTv.setSingleLine(false);
            }
            setInputType(inputType);
        }
    }

    public BaseView(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(getViewResourceId(), this);
        mContext = context;
        labelTv = mView.findViewById(R.id.labelTv);
        valueTv = mView.findViewById(R.id.valueTv);
    }


    public void setDefaultValue(String defaultValue) {
        Log.e("setDefaultValue", "defaultValue=" + defaultValue);
        if (null == valueTv) {
            return;
        }

        StringBuilder hintBuilder = new StringBuilder();
        switch (type) {
            case "edit":
                hintBuilder.append("请输入");
                break;
            case "scanandedit":
            case "recoveredit":
                hintBuilder.append("点击输入或扫描");
                break;
            case "spinner":
            case "datetime":
            case "date":
                hintBuilder.append("点击选择");
                break;
            case "base64photo":
                break;
            case "photo":
                break;
        }
        hintBuilder.append(label);
        if (!TextUtils.isEmpty(defaultValue)) {
            valueTv.setText(defaultValue);
        }
        valueTv.setHint(hintBuilder.toString());
    }

    public abstract int getViewResourceId();

    public String getValue() {
        return valueTv.getText().toString();
    }

    public String getLabel() {
        return label;
    }

    public String getParamer() {
        return paramer;
    }

    public void setBaseInfo(String label, String defaultValue, String parentParamer, String paramer, String mustParamer, String relevanceParamer, boolean isMust, String type) {
        this.parentParamer = parentParamer;
        this.paramer = paramer;
        this.relevanceParamer = relevanceParamer;
        this.isMust = isMust;
        this.type = type;
        this.label = label;
        this.defaultValue = defaultValue;
        this.mustParamer = mustParamer;
        labelTv.setText(label);
        setMust(isMust);
        setDefaultValue(defaultValue);
    }

    public boolean isMust() {
        return isMust;
    }

    public void setMust(boolean must) {
        isMust = must;
        if (isMust) {
            Drawable drawable = getResources().getDrawable(R.drawable.xing);
            drawable.setBounds(0, 0, 32, 32);
            labelTv.setCompoundDrawables(null, null, drawable, null);
        } else {
            labelTv.setCompoundDrawables(null, null, null, null);

        }
    }

    public void setInputType(int type) {
        Log.e("setInputType", "type=" + type);
        switch (type) {
            case 0:
                valueTv.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 1:
                valueTv.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
                break;
            case 2:
                valueTv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case 3:
                valueTv.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                break;
            case 4:
                valueTv.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            case 5:
                valueTv.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
        }
    }

    public String getRelevanceParamer() {
        return relevanceParamer;
    }

    public String getMustParamer() {
        return mustParamer;
    }


    public String getType() {
        return type;
    }

    public void reLoad(List<String> values) {
        if (values != null && values.size() > 0) {
            valueTv.setText(values.get(0));
        } else {
            valueTv.setText("");
        }
    }

    public void reLoad() {
        valueTv.setText("");
    }

    public void reValue(String value) {
        valueTv.setText(value);
    }

    public void reValue(String value, boolean clickAble) {
        valueTv.setText(value);
        mView.setClickable(clickAble);
        valueTv.setText(value);
        valueTv.setFocusable(clickAble);
        valueTv.setFocusableInTouchMode(clickAble);
        valueTv.setClickable(clickAble);
    }
    public void setClickAble(boolean clickAble) {
        mView.setClickable(clickAble);
        valueTv.setFocusable(clickAble);
        valueTv.setFocusableInTouchMode(clickAble);
        valueTv.setClickable(clickAble);
    }

    public void setLabel(String label) {
        this.label = label;
        labelTv.setText(label);
    }


    public void setLabelColor(int labelColor){
        labelTv.setTextColor(labelColor);
    }


}
